package com.bwie.auth.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.bwie.auth.service.AuthService;
import com.bwie.common.constants.JwtConstants;
import com.bwie.common.constants.TokenConstants;
import com.bwie.common.domain.User;
import com.bwie.common.domain.request.UserRequest;
import com.bwie.common.domain.response.UserResponse;
import com.bwie.common.redis.RedisCache;
import com.bwie.common.remote.system.RemoteUserService;
import com.bwie.common.result.Result;
import com.bwie.common.utils.IdUtils;
import com.bwie.common.utils.JwtUtils;
import com.bwie.common.utils.MsgUitl;
import lombok.experimental.Tolerate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.servlet.http.HttpServletRequest;
import javax.swing.text.JTextComponent;
import java.util.HashMap;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @Author：ShenJiaFeng
 * @Package：com.bwie.auth.service.impl
 * @Project：zg5_19
 * @name：AuthServiceImpl
 * @Date：2024/2/17 14:21
 */
@Service
public class AuthServiceImpl implements AuthService {

    private final RedisCache redisCache;
    private final StringRedisTemplate redisTemplate;
    private final HttpServletRequest request;
    private final RemoteUserService remoteUserService;

    public AuthServiceImpl(RedisCache redisCache, StringRedisTemplate redisTemplate, HttpServletRequest request, RemoteUserService remoteUserService) {
        this.redisCache = redisCache;
        this.redisTemplate = redisTemplate;
        this.request = request;
        this.remoteUserService = remoteUserService;
    }

    @Override
    public Result<UserResponse> login(UserRequest userRequest) {
        Result<User> byUserName = remoteUserService.findByUserName(userRequest.getUserName());
        Assert.isTrue(byUserName.isSuccess(),"登录失败");
        User user = byUserName.getData();
        Assert.notNull(user,"用户不存在");
        Assert.isTrue(userRequest.getUserPwd().equals(user.getUserPwd()),"密码错误");
        HashMap<String, Object> map = new HashMap<>();
        String userKey = IdUtils.genId();
        map.put(JwtConstants.USER_KEY,userKey);
        map.put(JwtConstants.DETAILS_USER_ID,user.getUserId());
        map.put(JwtConstants.DETAILS_USERNAME,user.getUserName());
        String token = JwtUtils.createToken(map);

        redisCache.setCacheObject(
                TokenConstants.LOGIN_TOKEN_KEY+userKey,
                user,
                TokenConstants.EXPIRATION,
                TimeUnit.MINUTES
        );
        return Result.success(
                UserResponse.builder()
                        .token(token)
                        .outTime(TokenConstants.EXPIRATION)
                        .build()
        );
    }

    @Override
    public User userInfo() {
      return redisCache.getCacheObject(
              TokenConstants.LOGIN_TOKEN_KEY+request.getHeader(JwtConstants.USER_KEY)
      );
    }

    @Override
    public void deleteToken() {
       redisCache.deleteObject(
               TokenConstants.LOGIN_TOKEN_KEY+request.getHeader(JwtConstants.USER_KEY)
       );
    }

//    @Override
//    public Result sendCode(String userName) {
//        Result<User> byUserName = remoteUserService.findByUserName(userName);
//        User user = byUserName.getData();
//        Assert.notNull(user,"用户不存在无法发送验证码");
//        String code = RandomUtil.randomNumbers(4);
//        redisCache.setCacheObject(userName,code);
//        return Result.success(code,"发送成功");
//    }





}






